package cl.whyem.beans.navigationBasicsManagedBeans1;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import org.apache.commons.validator.GenericValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cl.whyem.dto.CustomerDTO;
import cl.whyem.service.CustomerLookup;

@ManagedBean
public class InicioFormBean implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(InicioFormBean.class);
	
	private Integer balanceActual;
	
	private String usuario;
	private String password;
	
	@ManagedProperty(value="#{customerLookupImpl}")
	private CustomerLookup customerLookup;
	
	public void iniciar() {
	}
	
	public String calcularBalanceTotal() {
		CustomerDTO customerDTO = customerLookup.findByUsuario(usuario);
		logger.info("Customer DTO --> "+customerDTO.toString());
		
		if(!password.equals("123") || GenericValidator.isBlankOrNull(usuario) || customerDTO == null) {
			return "error";
		}
		
		String [] paginas = {"perdida","ganancia"};
		return customerDTO.getBalance() < 0 ? paginas[0] : paginas[1];
	}
	
	/* GETTERS Y SETTERS */

	public Integer getBalanceActual() {
		return balanceActual;
	}

	public void setBalanceActual(Integer balanceActual) {
		this.balanceActual = balanceActual;
	}

	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getUsuario() {
		return usuario;
	}
	
	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}

	public void setCustomerLookup(CustomerLookup customerLookup) {
		this.customerLookup = customerLookup;
	}
}
